<?php

namespace App\Transformers\Mengborun;

use App\Models\Mengborun\User;
use League\Fractal\ParamBag;
use League\Fractal\TransformerAbstract;
use Log;

class UserTransformer extends TransformerAbstract
{
//    protected $availableIncludes = ['user',  'recentComments'];

    public function transform(User $user)
    {
//        return $user->attributesToArray();
//Log::info($user);
//        $address = $user->userAddress();
//Log::info(json_encode($address));

        return [
            'id' => $user->id,
            'truename' => $user->truename,
            'company' => $user->company,
            'address' => $user->address,
//            'tel' => $address->tel,
//            'province' => $address->province,
//            'city' => $address->city,
//            'area' => $address->area,
//            'details_address' => $address->details_address,
            'points' => $user->point,
        ];
    }

//  如果有包含子对象，可重写此方法
//    public function includeUser(Post $user)
//    {
//        if (! $post->user) {
//            return $this->null();
//        }
//
//        return $this->item($post->user, new UserTransformer());
//    }


    /**
     * 列表加载列表不是一件很好的事情，因为dingo的预加载机制
     * 自动预加载include的参数, 所以会读取所有帖子的所有评论
     * 所以可以增加一个recentComments, 增加一个limit条件
     * 但是依然不够完美.
     */
//    public function includeRecentComments(Post $post)
//    {
//        // 倒叙
//        $comments = $post->recentComments->sortByDesc('id');
//
//        return $this->collection($comments, new CommentTransformer())
//            ->setMeta([
//                'count' => $comments->count(),
//            ]);
//    }
}
